<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<form name="$ctrl.formResponseTemplate" novalidate>
  <div class="gv-forms" layout="column">
    <div class="gv-forms-header">
      <h1 ng-if="$ctrl.creation">New Response Template</h1>
      <h1 ng-if="!$ctrl.creation">Response Template: {{$ctrl.templateKey}}</h1>
      <a ui-sref="management.apis.detail.proxy.responsetemplates.list">Back to response templates</a>
    </div>

    <div class="gv-form" ng-if="$ctrl.creation">
      <div class="gv-forms-header">
        <h2>Define the key</h2>
      </div>

      <div class="gv-form-content">
        <!-- Template Key -->
        <div flex-xs flex="50">
          <md-autocomplete
            flex
            required
            md-input-name="autocompleteTemplateKeyField"
            md-selected-item="$ctrl.selectedTemplateKey"
            md-selected-item-change="$ctrl.onSelectedTemplateKey(key)"
            md-search-text="$ctrl.searchTemplateKey"
            md-items="key in $ctrl.querySearchTemplateKey($ctrl.searchTemplateKey)"
            md-item-text="key"
            md-min-length="0"
            md-require-match="false"
            md-floating-label="Template key"
          >
            <md-item-template>
              <span md-highlight-text="$ctrl.searchTemplateKey" md-highlight-flags="^i">{{key}}</span>
            </md-item-template>
          </md-autocomplete>
          <div class="hint">The template key for which template responses will be applied.</div>
        </div>
      </div>
    </div>

    <gv-response-template-type ng-repeat="template in $ctrl.templates" template="template"> </gv-response-template-type>

    <md-content layout="row">
      <md-button class="md-raised" permission permission-only="'api-response_templates-u'" ng-click="$ctrl.addTemplate()">
        Add a new response template
      </md-button>
    </md-content>

    <div class="md-actions gravitee-api-save-button" layout="row">
      <md-button
        permission
        permission-only="'api-response_templates-u'"
        class="md-raised md-primary"
        ng-click="$ctrl.update()"
        ng-disabled="$ctrl.formResponseTemplate.$invalid || $ctrl.formResponseTemplate.$pristine"
      >
        Save
      </md-button>
      <md-button
        permission
        permission-only="'api-response_templates-u'"
        class="md-raised"
        type="button"
        ng-click="$ctrl.reset()"
        ng-disabled="$ctrl.formResponseTemplate.$pristine"
      >
        Reset
      </md-button>
    </div>
  </div>
</form>
